//package com.plian.system.service.dj.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.config.YthTaskObserver;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.entity.dj.DjPartyOrganizationMap;
//import com.plian.system.entity.sys.Org;
//import com.plian.system.mapper.dj.DjPartyOrganizationMapMapper;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.service.dj.IDjPartyOrganizationMapService;
//import com.plian.system.service.sys.code.IFormCodeService;
//import com.plian.system.service.sys.orgzation.IOrgService;
//import com.plian.system.service.sys.user.ISysUserService;
//import com.plian.system.service.wf.FormWorkflowService;
//import com.plian.system.service.yth.IPartyOrgMapService;
//import com.plian.system.vo.dj.DjPartyOrganizationMapVO;
//import com.plian.system.wrapper.dj.DjPartyOrganizationMapWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//import java.util.stream.Collectors;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//
///**
// * 党建组织地图管理
// **/
//@Slf4j
//@Service
//@AllArgsConstructor
//public class DjPartyOrganizationMapServiceImpl extends BaseServiceImpl<DjPartyOrganizationMapMapper, DjPartyOrganizationMap> implements IDjPartyOrganizationMapService, BaseFlowService {
//
//
//    private FormWorkflowService formWorkflowService;
//
//    private IFormCodeService formCodeService;
//
//    private final DjPartyOrganizationMapWrapper partyOrganizationMapWrapper;
//
//    private ISysUserService userService;
//
//    private IOrgService orgService;
//
//    private IPartyOrgMapService partyOrgMapService;
//
//
//    @Override
//    @Transactional(rollbackFor = Throwable.class)
//    public boolean submit(DjPartyOrganizationMap partyOrganizationMap) {
//        if (!Optional.ofNullable(partyOrganizationMap.getStatus()).isPresent()) {
//            partyOrganizationMap.setStatus(FormStatusConstant.UNREVIEWED);
//        }
//        return saveOrUpdate(partyOrganizationMap);
//    }
//
//    @Override
//    public boolean saveOrUpdate(DjPartyOrganizationMap partyOrganizationMap) {
//        boolean flag = false;
//        if (partyOrganizationMap.getId() == null) {
//            flag = true;
//            partyOrganizationMap.setCode(formCodeService.getCode(FormTypeConstant.DJ_PARTYORGANIZATIONMAP_EVALUATION));
//            partyOrganizationMap.setFormType(String.valueOf(FormTypeConstant.DJ_PARTYORGANIZATIONMAP_EVALUATION));
//        }
//        boolean result = super.saveOrUpdate(partyOrganizationMap);
//        return result;
//    }
//
//
//    @Override
//    public boolean deleteLogic(List<Long> ids) {
//        for (Long id : ids) {
//            DjPartyOrganizationMap partyOrganizationMap = getById(id);
//            if (partyOrganizationMap == null) {
//                return false;
//            } else if (partyOrganizationMap.getStatus() != null &&
//                    (partyOrganizationMap.getStatus() != FormStatusConstant.STASH && partyOrganizationMap.getStatus() != FormStatusConstant.UNREVIEWED)) {
//                return false;
//            }
//            formWorkflowService.deleteProcessInstance(String.valueOf(id));
//        }
//        boolean result = super.deleteLogic(ids);
//        return result;
//    }
//
//    @Override
//    public HashMap<String, Object> commit(String id) {
//        handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//        //将需要提交的流程变量返回，用于工作流侧
//        return new HashMap<String, Object>();
//    }
//
//    @Override
//    public Boolean finish(String id) {
//        /**
//         * 设置审批人和审批时间
//         */
//        DjPartyOrganizationMap partyOrganizationMap = getById(id);
//        partyOrganizationMap.setApprovedUser(TokenUtil.getTokenUserId());
//        Date now = DateUtil.now();
//        partyOrganizationMap.setApprovedTime(now);
//        updateById(partyOrganizationMap);
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//    @Override
//    public Boolean reject(String id, int formState, String comment) {
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//    @Override
//    public List<PageData> findByIds(List<String> formIds) {
//        List<DjPartyOrganizationMap> djPartymemberBaseinfos = list(new LambdaQueryWrapper<DjPartyOrganizationMap>()
//                .in(DjPartyOrganizationMap::getId, Func.toLongList(StringUtils.join(formIds, StringPool.COMMA))));
//        List<DjPartyOrganizationMapVO> djPartymemberBaseinfoVOList = partyOrganizationMapWrapper.entityToVO(djPartymemberBaseinfos);
//        Org org = orgService.getById(TokenUtil.getTokenOrgId());
//        List<PageData> pageDataList = new ArrayList<>();
//        try {
//            if (CollectionUtil.isNotEmpty(djPartymemberBaseinfoVOList)) {
//                for (DjPartyOrganizationMap vo : djPartymemberBaseinfoVOList) {
//                    PageData pd = new PageData();
//                    pd.put("id", vo.getId());
//                    pd.put("UNITNAME", org.getName());
//                    pd.put("APPLICANTNAME", vo.getCreateUser());
//                    pd.put("applicantId", vo.getCreateUser());
//                    pd.put("applyOrganizationId", org.getId());
//                    pd.put("formType", FormTypeConstant.DJ_PARTYORGANIZATIONMAP_EVALUATION);
//                    pd.put("orgName", org.getName());
//                    pageDataList.add(pd);
//                }
//            }
//            return pageDataList;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//        }
//        return pageDataList;
//    }
//
//    @Override
//    public List<Integer> getFormType() {
//        return Arrays.asList(FormTypeConstant.DJ_PARTYORGANIZATIONMAP_EVALUATION);
//    }
//
//    @Override
//    public void goBackToStash(String formId) {
//        handleStatus(Long.valueOf(formId), UNREVIEWED);
//    }
//
//    /**
//     * 统一处理状态机事件方法
//     *
//     * @param id           主键
//     * @param updateStatus 更新状态
//     * @return boolean
//     */
//    private Boolean handleStatus(Long id, int updateStatus) {
//        try {
//            DjPartyOrganizationMap partyOrganizationMap = getById(id);
//            partyOrganizationMap.setStatus(updateStatus);
//            updateById(partyOrganizationMap);
//            return true;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//
//    @Override
//    @YthTaskObserver
//    public boolean saveDjPartyOrganizationMapByDataSource(String date) {
//        boolean flag = true;
//        Date start = DateUtil.parse(date, DateUtil.PATTERN_DATE);
//        //获取数据
//        //List<DjPartyOrganizationMap> syncDatas = partyOrgMapService.getSyncData(start, DateUtil.plusDays(start, 1));
//        List<DjPartyOrganizationMap> syncDatas = partyOrgMapService.getSyncData(null, null);
//
//        Map<String, String> orgSourceIds = orgService.getSourceId();
//        Map<String, String> userSourceIds = userService.getSourceId();
//
//        if (CollectionUtil.isNotEmpty(syncDatas)) {
//
//            //查询数据库存在的旧数据
//            List<DjPartyOrganizationMap> allOldData = list(new LambdaQueryWrapper<DjPartyOrganizationMap>()
//                    .eq(DjPartyOrganizationMap::getDataSource, 3)
//                    .in(DjPartyOrganizationMap::getDataSourceId, syncDatas.stream().map(DjPartyOrganizationMap::getDataSourceId).collect(Collectors.toList())));
//
//            for (DjPartyOrganizationMap syncData : syncDatas) {
//                //企业
//                String partyOrganizationCode = orgSourceIds.get(syncData.getPartyOrganizationCode());
//                if (StringUtils.isNotBlank(partyOrganizationCode)) {
//                    syncData.setPartyOrganizationCode(partyOrganizationCode);
//                    syncData.setCreateCompanyId(partyOrganizationCode);
//                }
//                if (CollectionUtil.isNotEmpty(allOldData)) {
//                    Optional<DjPartyOrganizationMap> oldDjPartyOrganizationMapOptional = allOldData.stream().filter(e -> syncData.getDataSourceId().equals(e.getDataSourceId())).findFirst();
//                    if (oldDjPartyOrganizationMapOptional.isPresent()) {
//                        DjPartyOrganizationMap organizationMap = oldDjPartyOrganizationMapOptional.get();
//                        organizationMap.setPartyOrganizationName(syncData.getPartyOrganizationName());
//                        organizationMap.setPartyOrganizationCode(syncData.getPartyOrganizationCode());
//                        organizationMap.setPartyOrganizationAddr(syncData.getPartyOrganizationAddr());
//                        organizationMap.setPartyOrganizationDesp(syncData.getPartyOrganizationDesp());
//                        flag = updateById(organizationMap);
//                    } else {
//                        flag = super.saveOrUpdate(syncData);
//                    }
//                } else {
//                    flag = super.saveOrUpdate(syncData);
//                }
//            }
//        }
//        return flag;
//
//    }
//}
